package com.zhihuichongcheng.application.receiver;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.smartlib.cmnObject.util.LogUtil;
import com.zhihuichongcheng.application.ui.approval.activity.ApprovalListActivity;
import com.zhihuichongcheng.application.ui.im.activity.NewsActivity;
import com.zhihuichongcheng.application.ui.im.activity.NoticeActivity;
import com.zhihuichongcheng.application.ui.mine.LoginActivity;
import com.zhihuichongcheng.application.ui.task.TaskListActivity;
import com.zhihuichongcheng.application.ui.task.TaskMsgActivity;
import com.xiaomi.mipush.sdk.ErrorCode;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;

import org.json.JSONObject;

import java.util.List;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

/**
 * Created by Administrator on 2016/10/13.
 */

public class MessageReceiver extends PushMessageReceiver {
    private String mRegId;
    private long mResultCode = -1;
    private String mReason;
    private String mCommand;
    private String mMessage;
    private String mTopic;
    private String mAlias;
    private String mUserAccount;
    private String mStartTime;
    private String mEndTime;

    private String TAG = "--XMPUSH--";

    @Override
    public void onReceivePassThroughMessage(Context context, MiPushMessage message) {
        LogUtil.logD(TAG + "onReceivePassThroughMessage   " + message.toString());
        mMessage = message.getContent();
        if (!TextUtils.isEmpty(message.getTopic())) {
            mTopic = message.getTopic();
        } else if (!TextUtils.isEmpty(message.getAlias())) {
            mAlias = message.getAlias();
        } else if (!TextUtils.isEmpty(message.getUserAccount())) {
            mUserAccount = message.getUserAccount();
        }
    }

    @Override
    public void onNotificationMessageClicked(Context context, MiPushMessage message) {
        mMessage = message.getContent();
        LogUtil.logD(TAG + "onNotificationMessageClicked   " + message.toString());
        try {
            JSONObject object = new JSONObject(mMessage);
            int type = object.getInt("type");
            if (type == 4) {

            } else if (type == 1) {
                Intent intent = new Intent(context, NewsActivity.class);
                intent.putExtra("isFromPush", true);
                intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } else if (type == 2) {
                Intent intent = new Intent(context, NoticeActivity.class);
                intent.putExtra("title", "公告通知");
                intent.putExtra("isFromPush", true);
                intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } else if (type == 3) {
                Intent intent = new Intent(context, NoticeActivity.class);
                intent.putExtra("title", "会议通知");
                intent.putExtra("isFromPush", true);
                intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } else if (type == 5) {
                Intent intent = new Intent(context, ApprovalListActivity.class);
                intent.putExtra("type", 0);
                intent.putExtra("title", "待我审批");
                intent.putExtra("isFromPush", true);
                intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } else if (type == 6) {
                Intent intent = new Intent(context, TaskMsgActivity.class);
                intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!TextUtils.isEmpty(message.getTopic())) {
            mTopic = message.getTopic();
        } else if (!TextUtils.isEmpty(message.getAlias())) {
            mAlias = message.getAlias();
        } else if (!TextUtils.isEmpty(message.getUserAccount())) {
            mUserAccount = message.getUserAccount();
        }

    }

    @Override
    public void onNotificationMessageArrived(Context context, MiPushMessage message) {
        mMessage = message.getContent();
        LogUtil.logD(TAG + "onNotificationMessageArrived   " + message.toString());
        LogUtil.logD(mMessage + "message");
        try {
            JSONObject object = new JSONObject(mMessage);
            int type = object.getInt("type");
            if (type == 4) {
                Intent intent = new Intent(context, LoginActivity.class);
                intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                MiPushClient.setAlias(context, "", null);
            } else if (type == 6) {
                Intent intent = new Intent();
                intent.setAction(TaskListActivity.ACTION_NEW_TASK_MSG);
                context.sendBroadcast(intent);
            } else {
                Intent intent = new Intent();
                intent.putExtra("type", type);
                intent.setAction("com.threeWater.oa");
                context.sendBroadcast(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onCommandResult(Context context, MiPushCommandMessage message) {
        LogUtil.logD(TAG + "onCommandResult   " + message.toString());
        String command = message.getCommand();
        List<String> arguments = message.getCommandArguments();
        String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments.get(1) : null);
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mRegId = cmdArg1;
            }
        } else if (MiPushClient.COMMAND_SET_ALIAS.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mAlias = cmdArg1;
            }
        } else if (MiPushClient.COMMAND_UNSET_ALIAS.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mAlias = cmdArg1;
            }
        } else if (MiPushClient.COMMAND_SUBSCRIBE_TOPIC.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mTopic = cmdArg1;
            }
        } else if (MiPushClient.COMMAND_UNSUBSCRIBE_TOPIC.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mTopic = cmdArg1;
            }
        } else if (MiPushClient.COMMAND_SET_ACCEPT_TIME.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mStartTime = cmdArg1;
                mEndTime = cmdArg2;
            }
        }
    }

    @Override
    public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) {
        LogUtil.logD(TAG + "onReceiveRegisterResult   " + message.toString());
        String command = message.getCommand();
        List<String> arguments = message.getCommandArguments();
        String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments.get(1) : null);
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mRegId = cmdArg1;
            }
        }
    }

}
